package com.gdzn.signal.mongo.service.impl;

import com.gdzn.common.mongo.filter.MongoQueryBuilder;
import com.gdzn.signal.mongo.document.DevCabinetMongo;
import com.gdzn.signal.mongo.repository.DevCabinetMongoRepository;
import com.gdzn.signal.mongo.service.IDevCabinetMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class DevCabinetMongoServiceImpl implements IDevCabinetMongoService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private DevCabinetMongoRepository devCabinetRepository;

    ExampleMatcher matcher = ExampleMatcher.matching()
//           .withMatcher("stationName",ExampleMatcher.GenericPropertyMatchers.contains())
            .withIgnorePaths("_class").withIncludeNullValues()
            .withMatcher("devId", ExampleMatcher.GenericPropertyMatchers.contains());


    @Override
    public DevCabinetMongo insert(DevCabinetMongo devCabinet) {
        return devCabinetRepository.save(devCabinet);
    }

    @Override
    public List<DevCabinetMongo> insertAll(List<DevCabinetMongo> list) {
        return devCabinetRepository.saveAll(list);
    }

    @Override
    public void deleteById(String id) {
        devCabinetRepository.deleteById(id);
    }

    @Override
    public void deleteByIds(String[] ids) {
        devCabinetRepository.deleteAllById(Arrays.asList(ids));
    }

    @Override
    public DevCabinetMongo update(DevCabinetMongo devCabinet) {
        return devCabinetRepository.save(devCabinet);
    }

    @Override
    public void updateById(String id, Map<String, Object> updateMap) {
        // 查询条件
        Query query = Query.query(Criteria.where("_id").is(id));
        // 更新条件
        Update update = MongoQueryBuilder.buildUpdate(updateMap);
        mongoTemplate.updateFirst(query, update, DevCabinetMongo.class);
    }

    @Override
    public void updateByParam(Map<String, Object> queryMap, Map<String, Object> updateMap) {
        Criteria criteria = MongoQueryBuilder.buildCriteria(queryMap);
        Query query = Query.query(criteria);
        Update update = MongoQueryBuilder.buildUpdate(updateMap);
        mongoTemplate.updateMulti(query, update, DevCabinetMongo.class);
    }

    @Override
    public DevCabinetMongo findById(String id) {
        return devCabinetRepository.findById(id).orElse(null);
    }

    @Override
    public DevCabinetMongo findOne(DevCabinetMongo vo) {
        Example<DevCabinetMongo> example = Example.of(vo, matcher);
        return devCabinetRepository.findOne(example).orElse(null);
    }

    @Override
    public List<DevCabinetMongo> findAll() {
        return devCabinetRepository.findAll();
    }

    @Override
    public List<DevCabinetMongo> findAll(DevCabinetMongo vo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "updateTime");
        return findAll(vo, sort);
    }

    @Override
    public List<DevCabinetMongo> findAll(DevCabinetMongo vo, Sort sort) {
        Example<DevCabinetMongo> example = Example.of(vo, matcher);
        return devCabinetRepository.findAll(example, sort);
    }

    @Override
    public Page<DevCabinetMongo> findListPage(DevCabinetMongo devCabinet, int page, int size) {
        Example<DevCabinetMongo> example = Example.of(devCabinet, matcher);
        // 分页参数
        if (page <= 0) {
            page = 1;
        }
        page = page - 1;
        if (size <= 0) {
            size = 10;
        }
        Pageable pageable = PageRequest.of(page, size);
        Page<DevCabinetMongo> result = devCabinetRepository.findAll(example, pageable);
        return result;
    }
}
